package indi.kider.server.employee.service;

import indi.kider.common.constant.AppUserType;
import indi.kider.common.model.employee.Waiter;
import indi.kider.server.employee.mapper.WaiterMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;

import java.util.List;

/**
 * Created by shixj on 2018/4/26.
 */
@Slf4j
@Service
public class WaiterServiceImpl implements WaiterService {
    @Autowired
    private WaiterMapper waiterMapper;
    @Autowired
    private UserService userService;
    /**
     * 根据id获取
     *
     * @param id
     * @return
     */
    @Override
    public Waiter getWaiterById(Long id) {
        return waiterMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取分页查询
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<List<Waiter>> getWaiterPages(PageRequest<Waiter> request) {
        PageUtil.start(request);
        return PageUtil.getPageResponse(waiterMapper.getWaiterList(request.getQuery()));
    }

    /**
     * 不分页查询
     *
     * @param waiter
     * @return
     */
    @Override
    public List<Waiter> getWaiters(Waiter waiter) {
        return waiterMapper.select(waiter);
    }

    /**
     * 新增
     *
     * @param waiter
     * @return
     */
    @Override
    public Waiter add(Waiter waiter) throws Exception{
        if (waiter != null) {
            if (waiter.getId() == null || waiter.getId().equals(0L)) {
                //存在的手机号=======
                String phone = waiter.getPhone();
                Waiter temp = new Waiter();
                temp.setPhone(phone);
                int count = waiterMapper.selectCount(temp);
                if(count == 0 ){
                    waiter.setPwd("123456");
                    waiter.setId(null);
                    waiterMapper.insertSelective(waiter);
//                    userService.insert(phone,AppUserType.WAITER);
                }else{
                    throw new Exception("手机号码"+phone+"已被注册!");
                }
            }
        }
        return waiter;
    }

    /**
     * 更新
     *
     * @param waiter
     * @return
     */
    @Override
    public Waiter modify(Waiter waiter) throws Exception{
        if (waiter != null && waiter.getId() != null && waiter.getId() > 0L){
            String phone = waiter.getPhone();
            Waiter temp = new Waiter();
            temp.setPhone(phone);
            int count = waiterMapper.selectCount(temp);
            if(count == 0 ){
                waiterMapper.updateByPrimaryKeySelective(waiter);
            }else if(count == 1 ){
                Waiter update = waiterMapper.selectOne(temp);
                if(update.getId() == waiter.getId()){
                    waiterMapper.updateByPrimaryKeySelective(waiter);
                }else{
                    throw new Exception("手机号码"+phone+"已被注册!");
                }
            }
        }
        return waiter;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delById(Long id) {
        waiterMapper.deleteByPrimaryKey(id);
    }
}
